import { createPinia, defineStore } from 'pinia';
import { reactive, ref } from 'vue';
import request from '@/utils/request';

export const useCounterStore = defineStore('counter', () => {
  const open_id = ref(uni.getStorageSync('open_id') || '');
  const phone = ref(uni.getStorageSync('phone') || '');
  // 1=>用户端 2=>师傅端
  const is_type = ref(uni.getStorageSync('is_type') || '');
  const nick_name = ref('');
  const headimg = ref('');
  const userInfo = ref({});
  const workerData = ref({});
  const options = ref({});
  const subscriptionsSetting = ref({});
  // 初始化图片域名变量
  let imgDomain = '';

  // 微信小程序：区分开发版/体验版/正式版
  // #ifdef MP-WEIXIN
  if (__wxConfig.envVersion === 'develop') {
    imgDomain = 'http://192.168.1.7:3000/img/'; // 开发版域名
  } else {
    imgDomain = 'https://adm.jinjiangservice.com/img/'; // 体验版/正式版域名
  }
  // #endif
  // 跳转页面
  const to = (url) => {
    uni.navigateTo({
      url,
    });
  };
  // 菜单配置列表-用户
  const tabList = [
    {
      text: '首页',
      icon: `${imgDomain}foot1.png`,
      selectedIcon: `${imgDomain}foot1h.png`,
      path: '/pages/home/index',
    },
    {
      text: '全部服务',
      icon: `${imgDomain}foot2.png`,
      selectedIcon: `${imgDomain}foot2h.png`,
      path: '/pages/services/index',
      // badge: 3, // 显示徽章（数字3）
    },
    {
      text: '在线报修',
      icon: `${imgDomain}foot3.png`,
      selectedIcon: `${imgDomain}foot3h.png`,
      path: '/pages/onlineRepairRequest/index',
    },
    {
      text: '我的',
      icon: `${imgDomain}foot4.png`,
      selectedIcon: `${imgDomain}foot4h.png`,
      path: '/pages/my/index',
      // disabled: true // 可选：禁用该项
    },
  ];
  // 菜单配置列表-师傅
  const tabList2 = [
    {
      text: '首页',
      icon: `${imgDomain}foot5.png`,
      selectedIcon: `${imgDomain}foot5h.png`,
      path: '/pages/master/home/index',
    },
    {
      text: '订单',
      icon: `${imgDomain}foot6.png`,
      selectedIcon: `${imgDomain}foot6h.png`,
      path: '/pages/master/order/index',
      // badge: 3, // 显示徽章（数字3）
    },
    {
      text: '我的库存',
      icon: `${imgDomain}foot7.png`,
      selectedIcon: `${imgDomain}foot7h.png`,
      path: '/pages/master/inventory/index',
      // disabled: true // 可选：禁用该项
    },
    {
      text: '师傅推荐',
      icon: `${imgDomain}foot8.png`,
      selectedIcon: `${imgDomain}foot8h.png`,
      path: '/pages/master/recommend/index',
      // disabled: true // 可选：禁用该项
    },
  ];
  // 登录
  const getUserByOpenId = async () => {
    const data = await request('wxapp/login/getUserByOpenId');
    userInfo.value = data;
  };
  const getWorkerByOpenId = async () => {
    const data = await request('worker/login/getWorkerByOpenId');
    workerData.value = data;
  };

  // 获取系统参数
  const getParamValByKey = async (key) => {
    const data = await request('wxapp/login/getParamValByKey', {
      key,
    });
    return data.value;
  };
  const times = ref();
  const loading = ref(false);
  const parameterInfo = ref({});
  const customerServicePhone = ref(0);
  const complaint_hotlinePhone = ref(0);
  const addressInfo = ref({});
  const cityInfo = ref({});
  const messageData = ref({});
  getParamValByKey('customer_service_hotline').then((res) => {
    customerServicePhone.value = res;
  });

  // 退出登录逻辑
  const outFun = () => {
    uni.removeStorageSync('open_id');
    uni.removeStorageSync('phone');
    uni.removeStorageSync('userInfo');
    open_id.value = '';
    phone.value = '';
    userInfo.value = {};
    workerData.value = {};
  };
  // 师傅通知权限
  const tzqxFun = () => {
    uni.requestSubscribeMessage({
      tmplIds: [
        'WrCPvJR0hWlnuUJtepFtYQZ1dNGeESwtDdserfNkJsw',
        'bLdc3rHrTBhaRtDRhYcw5XI6oDOAppWo8C27UPpLT-g',
      ],

      complete(err) {
        console.log('点击按钮续订阅数量', err);
      },
    });
  };

  const navigateBackData = ref({});
  const balanceData = ref({});
  return {
    balanceData,
    navigateBackData,
    messageData,
    tzqxFun,
    times,
    subscriptionsSetting,
    cityInfo,
    outFun,
    is_type,
    getWorkerByOpenId,
    addressInfo,
    loading,
    getParamValByKey,
    imgDomain,
    complaint_hotlinePhone,
    customerServicePhone,
    parameterInfo,
    tabList,
    tabList2,
    to,
    getUserByOpenId,
    open_id,
    phone,
    nick_name,
    headimg,
    userInfo,
    workerData,
    options,
  };
});

const pinia = createPinia();

export default pinia;
